package dl.jf.web.action.auth;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import dl.jf.common.constants.Constants;
import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MRecord;
import dl.jf.common.model.MSubRecord;
import dl.jf.service.iservice.IRecordService;
import dl.jf.web.action.BaseAction;
import dl.jf.web.form.RecordCreateForm;
import dl.jf.web.form.RecordForm;
import dl.jf.web.util.Toolkit;

public class RecordCreateAction extends BaseAction {
	
	

	public RecordCreateAction() {
		super("RecordCreateAction");
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// form
	private RecordCreateForm recordCreateForm;

	public RecordCreateForm getRecordCreateForm() {
		return recordCreateForm;
	}

	public void setRecordCreateForm(RecordCreateForm recordCreateForm) {
		this.recordCreateForm = recordCreateForm;
	}

	private RecordForm recordForm;

	public RecordForm getRecordForm() {
		return recordForm;
	}

	public void setRecordForm(RecordForm recordForm) {
		this.recordForm = recordForm;
	}

	// service
	private IRecordService recordService;

	public void setRecordService(IRecordService recordService) {
		this.recordService = recordService;
	}

	@Override
	public String execute() throws Exception {
//		session = ActionContext.getContext().getSession();
//		request = ServletActionContext.getRequest();
		return initial();
	}

	public String initial() throws Exception {
		setRecordCreateForm(new RecordCreateForm());
		pushList();
		return SUCCESS;
	}

	public String submitRecord() throws Exception {
		List<MSubRecord> subRecordList = new ArrayList<MSubRecord>();
		
		String[] subSubjectArray = null;
		if(recordCreateForm.getSubSubject() != null){
			subSubjectArray = recordCreateForm.getSubSubject();
		}
		String[] subCountArray = null;
		if(recordCreateForm.getSubCount() != null){
			subCountArray = recordCreateForm.getSubCount();
		}
		String[] subDetailArray = null;
		if(recordCreateForm.getRecordDetail() != null){
			subCountArray = recordCreateForm.getRecordDetail();
		}
		String[] cardOrCashArray = null;
		if(recordCreateForm.getCardOrCash() != null){
			cardOrCashArray = recordCreateForm.getCardOrCash();
		}
		if(subSubjectArray != null && subSubjectArray.length > 0){
			Integer id = 1;
			for (int i = 0; i < subSubjectArray.length; i++) {
				if (Toolkit.isEmpty(subSubjectArray[i])) {
					continue;
				}
				MSubRecord item = new MSubRecord();
				item.setRecordSubId(id);
				id++;
				item.setContent(subSubjectArray[i]);
				item.setCount(Double.parseDouble(subCountArray[i]));
				item.setDetail(subDetailArray[i]);
				item.setCardOrCashCode(Integer.parseInt(cardOrCashArray[i]));
				subRecordList.add(item);
			}
		}
		
		MRecord record = new MRecord();
		record.setType1Code(Integer.parseInt(recordCreateForm
				.getSelectedType1()));
		record.setType2Code(Integer.parseInt(recordCreateForm
				.getSelectedType2()));
		record.setPrivacyCode(Integer.parseInt(recordCreateForm
				.getSelectedPrivacy()));
		record.setSubject(recordCreateForm.getSubject());
		record.setSum(Double.parseDouble(recordCreateForm.getTotalCount()));
		record.setComment(recordCreateForm.getComment());
//		getSession();
		record.setInsUser((String)session.get("userId"));
		record.setSubRecordList(subRecordList);
		try {
			recordService.saveRecord(record);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		addActionMessage("msg");
//		addActionError("error");
//		addFieldError("sss", "sdafas");
		return Constants.ACTION_RESULT_SUBMIT_DONE;
	}

	private void pushList() {
		recordCreateForm
				.setType1List(getCodeLabelListById(Constants.RECORD_TYPE1_CODE_ID));
		recordCreateForm
				.setType2List(getCodeLabelListById(Constants.RECORD_TYPE2_CODE_ID));
		recordCreateForm
				.setPrivacyList(getCodeLabelListById(Constants.RECORD_PRIVACY_CODE_ID));
		recordCreateForm
				.setCardOrCashList(getCodeLabelListById(Constants.CARD_OR_CASH_CODE_ID));
	}

	private String getCodeContent(Integer codeId, Integer code) {
		List<LabelValue> list = getCodeLabelListById(codeId);
		String result = "";
		for (LabelValue lv : list) {
			if (code == Integer.parseInt(lv.getValue())) {
				result = lv.getLabel();
				break;
			}
		}
		return result;
	}

//	private void getSession() {
//		session = ActionContext.getContext().getSession();
//		request = ServletActionContext.getRequest();
//	}
}
